package com.apes.scm.settle.payment.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.soe.metadata.BusinessAttributeValue;
import com.apes.scm.settle.payment.model.Payment;
import com.apes.scm.settle.payment.model.PaymentExecuteRecord;
import com.apes.scm.settle.payment.model.PaymentItem;
import com.apes.scm.settle.payment.repository.PaymentExecuteRecordRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 收付款执行记录
 *
 * @author wuq
 * @create 2018-12-27 9:53
 */
@Service("paymentExecuteRecordService")
public class PaymentExecuteRecordService extends DomainService {

    @Autowired
    PaymentExecuteRecordRepository paymentExecuteRecordRepository;

    public PaymentExecuteRecord create(SimpleRequest request){
        PaymentExecuteRecord paymentExecuteRecord = request.getO(PaymentExecuteRecord.class);
        paymentExecuteRecord.setState("create");
        return paymentExecuteRecordRepository.saveAndFlush(paymentExecuteRecord);
    }

    public PaymentExecuteRecord done(SimpleRequest request){
        PaymentExecuteRecord paymentExecuteRecord = request.getO(PaymentExecuteRecord.class);
        paymentExecuteRecord.setState("done");
        return  paymentExecuteRecordRepository.saveAndFlush(paymentExecuteRecord);
    }


    /*
    * 收付款执行记录自动生成付款单接口
    * */
    public Payment flowPayment(SimpleRequest request) {
        PaymentExecuteRecord paymentExecuteRecord = request.getO(PaymentExecuteRecord.class);
        Payment payment = new Payment();
        payment.setScene(paymentExecuteRecord.getScene());
        payment.setState("sent");
        payment.setCompany(paymentExecuteRecord.getCompany());
        payment.setPartner(paymentExecuteRecord.getPartner());
        BusinessAttributeValue paymentType = new BusinessAttributeValue();
        paymentType.setId("Payment.paymentType.invoice_payment");
        payment.setPaymentType(paymentType);
        payment.setNote("自动生成！");
        Set<PaymentItem> paymentItemSet = new HashSet<>();
        PaymentItem paymentItem = new PaymentItem();
        paymentItem.setAmount(paymentExecuteRecord.getAmount());
        paymentItem.setOrigin(paymentExecuteRecord.getOrigin());
        paymentItem.setOriginBusinessType(paymentExecuteRecord.getOriginBusinessType());
        paymentItem.setSource(paymentExecuteRecord.getSource());
        paymentItem.setPaymentExecuteRecord(paymentExecuteRecord);
        paymentItemSet.add(paymentItem);
        payment.setPaymentItemSet(paymentItemSet);
        return payment;
    }


    /**
     * 根据公司和合作伙伴查询执行记录
     * @param request
     * @return
     */
    public List<PaymentExecuteRecord> findAllForPayment(SimpleRequest request){
        String companyId = request.get("companyId");
        String partnerId = request.get("partnerId");
        return paymentExecuteRecordRepository.findAllForPayment(companyId, partnerId);
    }
}
